package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.PzdbgBsDetailBo;
import com.ruoyi.report_ljlm.domain.vo.PzdbgBsDetailVo;
import com.ruoyi.report_ljlm.domain.PzdbgBsDetail;
import com.ruoyi.report_ljlm.mapper.PzdbgBsDetailMapper;
import com.ruoyi.report_ljlm.service.IPzdbgBsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路基路面平整度试验检测报告(连续式平整度仪)详情Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class PzdbgBsDetailServiceImpl implements IPzdbgBsDetailService {

    private final PzdbgBsDetailMapper baseMapper;

    /**
     * 查询路基路面平整度试验检测报告(连续式平整度仪)详情
     */
    @Override
    public PzdbgBsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询路基路面平整度试验检测报告(连续式平整度仪)详情列表
     */
    @Override
    public TableDataInfo<PzdbgBsDetailVo> queryPageList(PzdbgBsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PzdbgBsDetail> lqw = buildQueryWrapper(bo);
        Page<PzdbgBsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路基路面平整度试验检测报告(连续式平整度仪)详情列表
     */
    @Override
    public List<PzdbgBsDetailVo> queryList(PzdbgBsDetailBo bo) {
        LambdaQueryWrapper<PzdbgBsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PzdbgBsDetail> buildQueryWrapper(PzdbgBsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PzdbgBsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, PzdbgBsDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getXvh()), PzdbgBsDetail::getXvh, bo.getXvh());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuangh()), PzdbgBsDetail::getZhuangh, bo.getZhuangh());
        lqw.eq(bo.getPingzdz() != null, PzdbgBsDetail::getPingzdz, bo.getPingzdz());
        return lqw;
    }

    /**
     * 新增路基路面平整度试验检测报告(连续式平整度仪)详情
     */
    @Override
    public Boolean insertByBo(PzdbgBsDetailBo bo) {
        PzdbgBsDetail add = BeanUtil.toBean(bo, PzdbgBsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改路基路面平整度试验检测报告(连续式平整度仪)详情
     */
    @Override
    public Boolean updateByBo(PzdbgBsDetailBo bo) {
        PzdbgBsDetail update = BeanUtil.toBean(bo, PzdbgBsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PzdbgBsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路基路面平整度试验检测报告(连续式平整度仪)详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
